﻿'========================================================================
'功能:
'作者:andy
'日期:
'========================================================================
Imports System.Data
Imports System.IO
Imports DataTableRenderToExcel
Partial Class WebForm_AssetMaintain_ReplyStatistics
    Inherits BasePage
    Const intMaintain As Integer = 0
    Const intEliminate As Integer = 1
    Const intTransfer As Integer = 2
    Const intFlower As Integer = 3
    Const intReply As Integer = 4
    Const intSOP As Integer = 5


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '  CType(Me.Master.FindControl("smBase"), ScriptManager).RegisterPostBackControl(btnExcel)
        If Not IsPostBack Then
            Dim bbl As OAMS.BBL.GroupBBL = New OAMS.BBL.GroupBBL()

            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            txtReturnDeadline1.Text = Today.ToString("yyyy/MM/01")
            txtReturnDeadline2.Text = DateTime.Parse(Today.AddMonths(1).ToString("yyyy/MM/01")).AddDays(-1).ToString("yyyy/MM/dd")

        End If

        CType(Me.Master.FindControl("smBase"), ScriptManager).RegisterPostBackControl(btnExcel)


    End Sub


  


    Protected Sub ddlKind_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlKind.SelectedIndexChanged
        'If ddlKind.SelectedValue = "1" Then
        '    gvMain.Columns(0).Visible = True
        '    gvMain.Columns(1).Visible = False
        'Else
        '    gvMain.Columns(1).Visible = True
        '    gvMain.Columns(0).Visible = False

        'End If
        'gvMain.DataSourceID = dbReplyStatistics.ID
        'gvMain.DataBind()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Try
            gvMain.Visible = False
            DateTime.Parse(txtReturnDeadline1.Text)
            DateTime.Parse(txtReturnDeadline2.Text)

        Catch ex As Exception
            ShowMessageForAjax(btnExcel, "Send Mail Date is wrong.")
            Exit Sub
        End Try


        If ddlKind.SelectedValue = "1" Then
            gvMain.Columns(0).Visible = True
            gvMain.Columns(1).Visible = False
        Else
            gvMain.Columns(1).Visible = True
            gvMain.Columns(0).Visible = False

        End If
        Try
            gvMain.Visible = True
            ' gvMain.DataSourceID = dbReplyStatistics.ID
            gvMain.DataBind()
        Catch ex As Exception
            ShowMessageForAjax(btnExcel, ex.Message)
            WriteErrLog("Query", ex.Message)
            Exit Sub
        End Try

    End Sub

    Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
        Try
            Dim table As New DataTable()
            Dim bbl As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()

            table = bbl.GetReplyStatistics(ddlKind.SelectedValue, ddlAssetDef.SelectedValue, txtDept.Text, DateTime.Parse(txtReturnDeadline1.Text), DateTime.Parse(txtReturnDeadline2.Text), ddlHouseID.SelectedValue, hiDBName.Value)

            If table IsNot Nothing AndAlso table.Rows.Count > 0 Then

                If ddlKind.SelectedValue = "1" Then
                    table.Columns(0).ColumnName = GetLocalResourceObject("colDept.HeaderText")
                    table.Columns(1).ColumnName = GetLocalResourceObject("lblAssetType.Text")

                    table.Columns(2).ColumnName = GetLocalResourceObject("colReplyQty.HeaderText")
                    table.Columns(3).ColumnName = GetLocalResourceObject("colReplyOK.HeaderText")
                    table.Columns(4).ColumnName = GetLocalResourceObject("colReplyRate.HeaderText")
                    table.Columns(5).ColumnName = GetLocalResourceObject("colAvgDays.HeaderText")
                    table.Columns(6).ColumnName = GetLocalResourceObject("assetdef_id")
                    table.Columns(7).ColumnName = GetLocalResourceObject("colQty1.HeaderText")
                    table.Columns(8).ColumnName = GetLocalResourceObject("colQty21.HeaderText")
                    table.Columns(9).ColumnName = GetLocalResourceObject("colQty22.HeaderText")
                    table.Columns(10).ColumnName = GetLocalResourceObject("colQty4.HeaderText")
                    table.Columns(11).ColumnName = GetLocalResourceObject("colQty7.HeaderText")

                End If
                If ddlKind.SelectedValue = "2" Then
                    table.Columns(0).ColumnName = GetLocalResourceObject("emp_no")
                    table.Columns(1).ColumnName = GetLocalResourceObject("assetdef_id")
                    table.Columns(2).ColumnName = GetLocalResourceObject("colDept1.HeaderText")
                    table.Columns(3).ColumnName = GetLocalResourceObject("lblAssetType.Text")

                    table.Columns(4).ColumnName = GetLocalResourceObject("colReplyQty.HeaderText")
                    table.Columns(5).ColumnName = GetLocalResourceObject("colReplyOK.HeaderText")
                    table.Columns(6).ColumnName = GetLocalResourceObject("colReplyRate.HeaderText")
                    table.Columns(7).ColumnName = GetLocalResourceObject("colAvgDays.HeaderText")
                    table.Columns(8).ColumnName = GetLocalResourceObject("colQty1.HeaderText")
                    table.Columns(9).ColumnName = GetLocalResourceObject("colQty21.HeaderText")
                    table.Columns(10).ColumnName = GetLocalResourceObject("colQty22.HeaderText")
                    table.Columns(11).ColumnName = GetLocalResourceObject("colQty4.HeaderText")
                    table.Columns(12).ColumnName = GetLocalResourceObject("colQty7.HeaderText")

                End If
                'table.Columns.RemoveAt(0);
             

                ' 產生 Excel 資料流。
                Dim ms As MemoryStream = TryCast(DataTableRenderToExcel.RenderDataTableToExcel(table), MemoryStream)
                ' 設定強制下載標頭。
                Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.xls", Server.UrlEncode("ReplyStatistics")))
                ' 輸出檔案。
                Response.BinaryWrite(ms.ToArray())

                ms.Close()
                ms.Dispose()
            Else
                'ShowMessageForAjax.ShowMessage(btnQuery, "沒有資料!")
            End If
            If table IsNot Nothing Then
                table.Dispose()

            End If
        Catch ex As Exception


        End Try



    End Sub


End Class
